package fun.flyee.android.pengshan.models;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;

import fun.flyee.android.pengshan.Storage;
import fun.flyee.android.pengshan.api.ApiClient;
import fun.flyee.android.pengshan.utils.DateUtil;
import fun.flyee.android.pengshan.utils.Logger;
import fun.flyee.android.pengshan.utils.Util;
import fun.flyee.android.pengshan.widgets.Tips;

public class GpsTimeSetting {

    public int seconds;

    public String status;

    public String amStartTime;
    public String amEndTime;
    public String pmStartTime;
    public String pmEndTime;

    public String date;

    public static void loadSetting(Callback callback) {
        ApiClient.get("/back-stage/teamAttendanceGps/info", new JSONObject(), (result) -> {
            if (result.code == 200) {
                JSONObject json = (JSONObject) result.data;
                json.put("date", DateUtil.getToday());
                GpsTimeSetting setting = JSONObject.parseObject(json.toString(), GpsTimeSetting.class);
                callback.onLoaded(setting);
            } else {
                Logger.e("加载巡查时间设置错误");
                callback.onLoaded(null);
            }
        });
    }

    public boolean checkInTime() {
//        long time = System.currentTimeMillis() / 1000 / 60 / 2;
//        return time % 2 == 0;
        if ("0".equals(status)) {
            long current = System.currentTimeMillis();
            String today = DateUtil.getToday();
            long amStart = DateUtil.parseTime(today + " " + amStartTime);
            long amEnd = DateUtil.parseTime(today + " " + amEndTime);
            long pmStart = DateUtil.parseTime(today + " " + pmStartTime);
            long pmEnd = DateUtil.parseTime(today + " " + pmEndTime);
            return (current > amStart && current < amEnd) || (current > pmStart && current < pmEnd);
        } else {
            return false;
        }
    }

    public interface Callback {
        void onLoaded(GpsTimeSetting setting);
    }

}
